package com.liveneo.adapter.mobile.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.geronimo.mail.util.Base64;
import sun.misc.BASE64Decoder;

/**
 * http 参数工具
 */
public class HttpParamUtil {
    /*****
     * 设置参数
     * @param parameterMap
     * @return
     */
    public static String getSortedUrl(Map<Object, Object> parameterMap) {
        try {
            Map<Object, Object> map = new TreeMap<Object, Object>(parameterMap);
            Set<Object> keySet = map.keySet();
            StringBuilder sb = new StringBuilder();
            for (Object key : keySet) {
                sb.append(key).append("=").append((String) map.get(key)).append("&");
            }
            String str = sb.substring(0, sb.length() - 1);
            return str;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * 设置参数 通过url地址中获得
     * @param params
     * @return
     */
    public static Map<String, Object> getMapbyUrlParams(String params) {
        try {
            Map<String, Object> map = new TreeMap<String, Object>();
            String[] ss = StringUtils.split(params, "&");
            for (String s : ss) {
                int index = s.indexOf("=");
                if (index > 0) {
                    String key = s.substring(0, index);
                    String value = s.substring(index + 1);
                    map.put(key, value);
                }
            }
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /****
     * 将参数字符串转成 二进制流
     * @param params 参数
     * @param encoding 编码方式
     * @return
     */
    public static InputStream xmlStringToByteInputStream(String params, String encoding) {
        InputStream in = null;
        try {
            in = new ByteArrayInputStream(params.getBytes(encoding));
            return in;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*****
     * 将输入的二进制流转成 字符串
     * @param inputStream
     * @return
     * @throws Exception
     */
    public static String xmlStringToInputStream(InputStream inputStream) throws Exception {
        ByteArrayOutputStream out = null;
        try {
            out = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int i = 0;
            while ((i = inputStream.read(b, 0, 1024)) > 0) {
                out.write(b, 0, i);
            }
            byte[] req = out.toByteArray();
            String str = new String(req, "UTF-8");
            return str.toString();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("解析失败：" + e.getMessage());
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /***
     * base64 编码 原生
     * @param data
     * @return
     */
    public static String base64Encrypt(String data, String encoding) {
        Base64 strs = new Base64();
        try {
            return new String(strs.encode(data.getBytes(encoding)));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    /*****
     * base64 解码 原生
     * @param s
     * @return
     */
    public static String base64Decrypt(String s, String encoding) {
        if (s == null) return null;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            return new String(decoder.decodeBuffer(s), encoding);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /******
     * 将参数采用 DesBase64 加密
     * @param params
     * @return
     */
    public static String paramEncryptByDesBase64(String params) {
        try {
            return DesBase64Util.encode(params.trim());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /******
     * 将参数采用 DesBase64 解密
     * @param params
     * @return
     */
    public static String paramDecoderByDesBase64(String params) {
        try {
            return DesBase64Util.decoder(params.trim());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws IOException {
        String data = "123456";
        String base64 = base64Encrypt(data, "utf-8");
        try {
            System.out.println(Base64.encode(data.getBytes("utf-8")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Object onj = new Object();
        onj = Base64.encode(data.getBytes("utf-8"));
        boolean isObject = onj instanceof byte[];
        System.out.println(isObject);
        System.out.println(base64);
        System.out.println(base64.getBytes());
    }
}
